package com.yyz.lease.web.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyz.lease.model.entity.ViewAppointment;
import com.yyz.lease.web.app.mapper.ViewAppointmentMapper;
import com.yyz.lease.web.app.service.ApartmentInfoService;
import com.yyz.lease.web.app.service.ViewAppointmentService;
import com.yyz.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.yyz.lease.web.app.vo.apartment.ApartmentItemVo;
import com.yyz.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.yyz.lease.web.app.vo.appointment.AppointmentItemVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 */
@Service
@RequiredArgsConstructor
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {
    private final ApartmentInfoService apartmentInfoService;

    /**
     * 查询个人预约看房列表
     */
    @Override
    public List<AppointmentItemVo> listItem() {
        List<ViewAppointment> viewAppointments = list();

        return viewAppointments.stream().map(
                viewAppointment -> {
                    ApartmentDetailVo apartmentDetailVo = apartmentInfoService.getDetailById(viewAppointment.getApartmentId());
                    AppointmentItemVo appointmentItemVo = new AppointmentItemVo();
                    appointmentItemVo.setId(viewAppointment.getId());
                    appointmentItemVo.setApartmentName(apartmentDetailVo.getName());
                    appointmentItemVo.setGraphVoList(apartmentDetailVo.getGraphVoList());
                    appointmentItemVo.setAppointmentTime(viewAppointment.getAppointmentTime());
                    appointmentItemVo.setAppointmentStatus(viewAppointment.getAppointmentStatus());
                    return appointmentItemVo;
                }
        ).toList();
    }

    /**
     * 根据ID查询预约详情信息
     */
    @Override
    public AppointmentDetailVo getDetailById(Long id) {
        ViewAppointment viewAppointment = getById(id);
        ApartmentItemVo apartmentItemVoById = apartmentInfoService.getApartmentItemVoById(viewAppointment.getApartmentId());

        AppointmentDetailVo appointmentDetailVo = new AppointmentDetailVo();
        BeanUtils.copyProperties(viewAppointment,appointmentDetailVo);
        appointmentDetailVo.setApartmentItemVo(apartmentItemVoById);

        return appointmentDetailVo;
    }
}




